//////////////////////////////////////////////////////////////////////////////////////////////////////////
//		            BulletSliderConstraint
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma warning (disable: 4819)
#include "CKAll.h"
#include "BulletManager.h"
//#include "VirtoolsMotionState.h"

CKObjectDeclaration	*FillBehaviorBulletSliderConstraintDecl();
CKERROR CreateBulletSliderConstraintProto(CKBehaviorPrototype **);
int BulletSliderConstraint(const CKBehaviorContext& BehContext);
//[!output BB_CALLBACKDECL]

CKObjectDeclaration	*FillBehaviorBulletSliderConstraintDecl()
{
	CKObjectDeclaration *od = CreateCKObjectDeclaration("BulletSliderConstraint");	
	
	od->SetType(CKDLL_BEHAVIORPROTOTYPE);
	od->SetVersion(0x00000001);
	od->SetCreationFunction(CreateBulletSliderConstraintProto);
	od->SetDescription("Bullet Slider Constraint");
	od->SetCategory("Bullet/Constraint");
	od->SetGuid(CKGUID(0x6d0c7311,0x45dc1c28));
	od->SetAuthorGuid(CKGUID(0x457d2f46,0x3ee809a8));
	od->SetAuthorName("gjfeilipu");
	od->SetCompatibleClassId(CKCID_BEOBJECT);

	return od;
}

CKERROR CreateBulletSliderConstraintProto(CKBehaviorPrototype** pproto)
{
	CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("BulletSliderConstraint");
	if (!proto) {
		return CKERR_OUTOFMEMORY;
	}

//[!output INPUT_DECL]
//[!output OUTPUT_DECL]
//[!output INPUTP_DECL]
//[!output OUTPUTP_DECL]
	proto->DeclareInput("In");
	proto->DeclareOutput("Out");

	proto->DeclareInParameter("Object_A",CKPGUID_3DENTITY);
	proto->DeclareInParameter("Object_B",CKPGUID_3DENTITY);
	proto->DeclareInParameter("Origin_A",CKPGUID_VECTOR,"0,0,0");
	proto->DeclareInParameter("Basis_A",CKPGUID_VECTOR,"0,0,0");
	proto->DeclareInParameter("Origin_B",CKPGUID_VECTOR,"0,0,0");
	proto->DeclareInParameter("Basis_B",CKPGUID_VECTOR,"0,0,0");
	proto->DeclareInParameter("ReferenceFrameA",CKPGUID_BOOL);
	proto->DeclareInParameter("DisableCollisionAandB",CKPGUID_BOOL);
	proto->DeclareInParameter("LowerLinLimit",CKPGUID_FLOAT,"0");
	proto->DeclareInParameter("UpperLinLimit",CKPGUID_FLOAT,"0");
	proto->DeclareInParameter("LowerAngLimit",CKPGUID_FLOAT,"0");
	proto->DeclareInParameter("UpperAngLimit",CKPGUID_FLOAT,"0");

	proto->DeclareOutParameter("Constraint ID",CKPGUID_INT,"-1");

//----	Local Parameters Declaration

//----	Settings Declaration
//[!output BB_FLAGS]
//[!output BB_CALLBACKREG]
	proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
	proto->SetFunction(BulletSliderConstraint);

	*pproto = proto;
	return CK_OK;
}

int BulletSliderConstraint(const CKBehaviorContext& BehContext)
{
	CKBehavior* beh = BehContext.Behavior;
	CKContext* cex = beh->GetCKContext();
	beh->ActivateInput(0,FALSE);
	beh->ActivateOutput(0,TRUE);
	
	BulletManager* bManager = (BulletManager*)cex->GetManagerByGuid(BulletManagerGUID);//->addRigidBody(btRigidBody *body);
	if(bManager)
	{
		btVector3 btOriginA, btOriginB, btBasisA, btBasisB;
		VxVector vtOriginA, vtOriginB, vtBasisA, vtBasisB;
		bool referenceFrameA;
		bool disableCollisionAandB;
		btScalar btLowerLinLimit, btUpperLinLimit, btLowerAngLimit, btUpperAngLimit;;

		beh->GetInputParameterValue(6, &referenceFrameA);
		beh->GetInputParameterValue(7, &disableCollisionAandB);
		beh->GetInputParameterValue(8, &btLowerLinLimit);
		beh->GetInputParameterValue(9, &btUpperLinLimit);
		beh->GetInputParameterValue(10, &btLowerAngLimit);
		beh->GetInputParameterValue(11, &btUpperAngLimit);

		beh->GetInputParameterValue(2, &vtOriginA);
		beh->GetInputParameterValue(3, &vtBasisA);
		beh->GetInputParameterValue(4, &vtOriginB);
		beh->GetInputParameterValue(5, &vtBasisB);

		
		btOriginA.setValue(vtOriginA.x, vtOriginA.y, -vtOriginA.z);
		btBasisA.setValue(vtBasisA.x, vtBasisA.y, -vtBasisA.z);
		btOriginB.setValue(vtOriginB.x, vtOriginB.y, -vtOriginB.z);
		btBasisB.setValue(vtBasisB.x, vtBasisB.y, -vtBasisB.z);

		//if(NULL !=body)
		{		
			CK3dEntity* obj_A = (CK3dEntity*)beh->GetInputParameterObject(0);
			CK3dEntity* obj_B = (CK3dEntity*)beh->GetInputParameterObject(1);
			btRigidBody* body_A =  bManager->findRigidBody(obj_A);
			btRigidBody* body_B =  bManager->findRigidBody(obj_B);
			int ID = -1;
			ID = bManager->addSliderConstraint(body_A, body_B, 
				btOriginA, btOriginB, btBasisA, btBasisB, referenceFrameA, disableCollisionAandB, 
				btLowerLinLimit, btUpperLinLimit, btLowerAngLimit, btUpperAngLimit);
			beh->SetOutputParameterValue(0, &ID);
		}
	}
	return CKBR_OK;
}